#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/4/15 14:56
# @Author  : Chenmm
# @File    : db.py
# @Software: PyCharm

from datetime import datetime
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class SysDept(db.Model):
    __tablename__ = 'sys_dept'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='主键')
    name = db.Column(db.String(100), nullable=False, comment='部门名称')
    code = db.Column(db.String(100), nullable=False, comment='部门编号')
    parent_id = db.Column(db.BigInteger, default=0, comment='父节点id')
    tree_path = db.Column(db.String(255), nullable=False, comment='父节点ID路径')
    sort = db.Column(db.SmallInteger, default=0, comment='显示顺序')
    status = db.Column(db.SmallInteger, default=1, comment='状态(1-正常 0-禁用)')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    create_time = db.Column(db.DateTime, comment='创建时间')
    update_by = db.Column(db.BigInteger, comment='修改人ID')
    update_time = db.Column(db.DateTime, comment='更新时间')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='逻辑删除标识')

    __table_args__ = (
        db.UniqueConstraint('code', name='uk_code'),
        {'comment': '部门表'}
    )


class SysDict(db.Model):
    __tablename__ = 'sys_dict'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='主键')
    dict_code = db.Column(db.String(50), comment='类型编码')
    name = db.Column(db.String(50), comment='类型名称')
    status = db.Column(db.SmallInteger, default=0, comment='状态')
    remark = db.Column(db.String(255), comment='备注')
    create_time = db.Column(db.DateTime, comment='创建时间')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    update_time = db.Column(db.DateTime, comment='更新时间')
    update_by = db.Column(db.BigInteger, comment='修改人ID')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='是否删除')

    __table_args__ = (
        db.Index('idx_dict_code', 'dict_code'),
        {'comment': '字典表'}
    )


class SysDictItem(db.Model):
    __tablename__ = 'sys_dict_item'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='主键')
    dict_code = db.Column(db.String(50), comment='关联字典编码')
    value = db.Column(db.String(50), comment='字典项值')
    label = db.Column(db.String(100), comment='字典项标签')
    tag_type = db.Column(db.String(50), comment='标签类型')
    status = db.Column(db.SmallInteger, default=0, comment='状态')
    sort = db.Column(db.Integer, default=0, comment='排序')
    remark = db.Column(db.String(255), comment='备注')
    create_time = db.Column(db.DateTime, comment='创建时间')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    update_time = db.Column(db.DateTime, comment='更新时间')
    update_by = db.Column(db.BigInteger, comment='修改人ID')

    __table_args__ = {'comment': '字典项表'}


class SysMenu(db.Model):
    __tablename__ = 'sys_menu'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='ID')
    parent_id = db.Column(db.BigInteger, nullable=False, comment='父菜单ID')
    tree_path = db.Column(db.String(255), comment='父节点ID路径')
    name = db.Column(db.String(64), nullable=False, comment='菜单名称')
    type = db.Column(db.SmallInteger, nullable=False, comment='菜单类型')
    route_name = db.Column(db.String(255), comment='路由名称')
    route_path = db.Column(db.String(128), comment='路由路径')
    component = db.Column(db.String(128), comment='组件路径')
    perm = db.Column(db.String(128), comment='权限标识')
    always_show = db.Column(db.SmallInteger, default=0, comment='是否始终显示')
    keep_alive = db.Column(db.SmallInteger, default=0, comment='是否开启缓存')
    visible = db.Column(db.SmallInteger, default=1, comment='显示状态')
    sort = db.Column(db.Integer, default=0, comment='排序')
    icon = db.Column(db.String(64), comment='菜单图标')
    redirect = db.Column(db.String(128), comment='跳转路径')
    create_time = db.Column(db.DateTime, comment='创建时间')
    update_time = db.Column(db.DateTime, comment='更新时间')
    params = db.Column(db.String(255), comment='路由参数')

    __table_args__ = {'comment': '菜单管理'}


class SysRole(db.Model):
    __tablename__ = 'sys_role'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64), nullable=False, comment='角色名称')
    code = db.Column(db.String(32), nullable=False, comment='角色编码')
    sort = db.Column(db.Integer, comment='显示顺序')
    status = db.Column(db.SmallInteger, default=1, comment='角色状态')
    data_scope = db.Column(db.SmallInteger, comment='数据权限')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    create_time = db.Column(db.DateTime, comment='创建时间')
    update_by = db.Column(db.BigInteger, comment='更新人ID')
    update_time = db.Column(db.DateTime, comment='更新时间')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='逻辑删除标识')

    __table_args__ = (
        db.UniqueConstraint('name', name='uk_name'),
        db.UniqueConstraint('code', name='uk_code'),
        {'comment': '角色表'}
    )


class SysRoleMenu(db.Model):
    __tablename__ = 'sys_role_menu'

    role_id = db.Column(db.BigInteger, primary_key=True, comment='角色ID')
    menu_id = db.Column(db.BigInteger, primary_key=True, comment='菜单ID')

    __table_args__ = (
        db.UniqueConstraint('role_id', 'menu_id', name='uk_roleid_menuid'),
        {'comment': '角色和菜单关联表'}
    )

class SysUser(db.Model):
    __tablename__ = 'sys_user'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    username = db.Column(db.String(64), unique=True, comment='用户名')
    nickname = db.Column(db.String(64), comment='昵称')
    gender = db.Column(db.SmallInteger, default=1, comment='性别((1-男 2-女 0-保密)')
    password = db.Column(db.String(100), comment='密码')
    dept_id = db.Column(db.Integer, comment='部门ID')
    avatar = db.Column(db.String(255), comment='用户头像')
    mobile = db.Column(db.String(20), comment='联系方式')
    status = db.Column(db.SmallInteger, default=1, comment='状态(1-正常 0-禁用)')
    email = db.Column(db.String(128), comment='用户邮箱')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    update_time = db.Column(db.DateTime, onupdate=datetime.now, comment='更新时间')
    update_by = db.Column(db.BigInteger, comment='修改人ID')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='逻辑删除标识(0-未删除 1-已删除)')
    openid = db.Column(db.String(28), comment='微信 openid')

    __table_args__ = (
        db.UniqueConstraint('username', name='login_name'),
        {'comment': '用户信息表'}
    )


class SysUserRole(db.Model):
    __tablename__ = 'sys_user_role'

    user_id = db.Column(db.BigInteger, primary_key=True, comment='用户ID')
    role_id = db.Column(db.BigInteger, primary_key=True, comment='角色ID')

    __table_args__ = {'comment': '用户和角色关联表'}


class SysLog(db.Model):
    __tablename__ = 'sys_log'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='主键')
    module = db.Column(db.String(50), nullable=False, comment='日志模块')
    request_method = db.Column(db.String(64), nullable=False, comment='请求方式')
    request_params = db.Column(db.Text, comment='请求参数')
    response_content = db.Column(db.Text, comment='返回参数')
    content = db.Column(db.String(255), nullable=False, comment='日志内容')
    request_uri = db.Column(db.String(255), comment='请求路径')
    method = db.Column(db.String(255), comment='方法名')
    ip = db.Column(db.String(45), comment='IP地址')
    province = db.Column(db.String(100), comment='省份')
    city = db.Column(db.String(100), comment='城市')
    execution_time = db.Column(db.BigInteger, comment='执行时间(ms)')
    browser = db.Column(db.String(100), comment='浏览器')
    browser_version = db.Column(db.String(100), comment='浏览器版本')
    os = db.Column(db.String(100), comment='终端系统')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='逻辑删除标识(1-已删除 0-未删除)')

    __table_args__ = (
        db.Index('idx_create_time', 'create_time'),
        {'comment': '系统日志表', 'mysql_engine': 'MyISAM'}
    )


class GenConfig(db.Model):
    __tablename__ = 'gen_config'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    table_name = db.Column(db.String(100), unique=True, nullable=False, comment='表名')
    module_name = db.Column(db.String(100), comment='模块名')
    package_name = db.Column(db.String(255), nullable=False, comment='包名')
    business_name = db.Column(db.String(100), nullable=False, comment='业务名')
    entity_name = db.Column(db.String(100), nullable=False, comment='实体类名')
    author = db.Column(db.String(50), nullable=False, comment='作者')
    parent_menu_id = db.Column(db.BigInteger, comment='上级菜单ID')
    create_time = db.Column(db.DateTime, comment='创建时间')
    update_time = db.Column(db.DateTime, comment='更新时间')
    is_deleted = db.Column(db.Boolean, default=False, comment='是否删除')

    __table_args__ = {'comment': '代码生成基础配置表'}


class GenFieldConfig(db.Model):
    __tablename__ = 'gen_field_config'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    config_id = db.Column(db.BigInteger, db.ForeignKey('gen_config.id'), nullable=False, comment='配置ID')
    column_name = db.Column(db.String(100), comment='列名')
    column_type = db.Column(db.String(50), comment='列类型')
    column_length = db.Column(db.Integer, comment='列长度')
    field_name = db.Column(db.String(100), nullable=False, comment='字段名称')
    field_type = db.Column(db.String(100), comment='字段类型')
    field_sort = db.Column(db.Integer, comment='字段排序')
    field_comment = db.Column(db.String(255), comment='字段描述')
    max_length = db.Column(db.Integer, comment='最大长度')
    is_required = db.Column(db.Boolean, comment='是否必填')
    is_show_in_list = db.Column(db.Boolean, default=False, comment='是否列表显示')
    is_show_in_form = db.Column(db.Boolean, default=False, comment='是否表单显示')
    is_show_in_query = db.Column(db.Boolean, default=False, comment='是否查询显示')
    query_type = db.Column(db.SmallInteger, comment='查询方式')
    form_type = db.Column(db.SmallInteger, comment='表单类型')
    dict_type = db.Column(db.String(50), comment='字典类型')
    create_time = db.Column(db.DateTime, comment='创建时间')
    update_time = db.Column(db.DateTime, comment='更新时间')

    __table_args__ = {'comment': '代码生成字段配置表'}


class SysConfig(db.Model):
    __tablename__ = 'sys_config'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    config_name = db.Column(db.String(50), nullable=False, comment='配置名称')
    config_key = db.Column(db.String(50), unique=True, nullable=False, comment='配置key')
    config_value = db.Column(db.String(100), nullable=False, comment='配置值')
    remark = db.Column(db.String(255), comment='备注')
    create_time = db.Column(db.DateTime, comment='创建时间')
    create_by = db.Column(db.BigInteger, comment='创建人ID')
    update_time = db.Column(db.DateTime, comment='更新时间')
    update_by = db.Column(db.BigInteger, comment='更新人ID')
    is_deleted = db.Column(db.SmallInteger, default=0, nullable=False, comment='逻辑删除标识')

    __table_args__ = {'comment': '系统配置表'}


class SysNotice(db.Model):
    __tablename__ = 'sys_notice'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    title = db.Column(db.String(50), comment='通知标题')
    content = db.Column(db.Text, comment='通知内容')
    type = db.Column(db.SmallInteger, nullable=False, comment='通知类型')
    level = db.Column(db.String(5), nullable=False, comment='通知等级')
    target_type = db.Column(db.SmallInteger, nullable=False, comment='目标类型')
    target_user_ids = db.Column(db.String(255), comment='目标用户ID')
    publisher_id = db.Column(db.BigInteger, comment='发布人ID')
    publish_status = db.Column(db.SmallInteger, default=0, comment='发布状态')
    publish_time = db.Column(db.DateTime, comment='发布时间')
    revoke_time = db.Column(db.DateTime, comment='撤回时间')
    create_by = db.Column(db.BigInteger, nullable=False, comment='创建人ID')
    create_time = db.Column(db.DateTime, nullable=False, comment='创建时间')
    update_by = db.Column(db.BigInteger, comment='更新人ID')
    update_time = db.Column(db.DateTime, comment='更新时间')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='是否删除')

    __table_args__ = {'comment': '通知公告表'}


class SysUserNotice(db.Model):
    __tablename__ = 'sys_user_notice'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='id')
    notice_id = db.Column(db.BigInteger, nullable=False, comment='通知ID')
    user_id = db.Column(db.BigInteger, nullable=False, comment='用户ID')
    is_read = db.Column(db.SmallInteger, default=0, comment='读取状态')
    read_time = db.Column(db.DateTime, comment='阅读时间')
    create_time = db.Column(db.DateTime, nullable=False, comment='创建时间')
    update_time = db.Column(db.DateTime, comment='更新时间')
    is_deleted = db.Column(db.SmallInteger, default=0, comment='逻辑删除')

    __table_args__ = {'comment': '用户通知公告表'}